package com.dpj.wait_test;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.TestCorrectPostureStep2")
public class TestCorrectPostureStep2 {
    static final Object room = new Object();
    static boolean hasCigarette = false; //有没有烟
    static boolean hasTakeout = false;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (room) {
                log.debug("有烟没？[{}]", hasCigarette);
                if (!hasCigarette) {
                    log.debug("没烟,先歇会儿!");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("有烟没？[{}]", hasCigarette);
                if (hasCigarette) {
                    log.debug("有烟,可以干活了!");
                }
            }
        },"小南").start();

        for(int i=0;i<5;i++){
            new Thread(()->{
                synchronized (room){
                    log.debug("可以开始干活了");
                }
            },"其他人").start();
        }
        Thread.sleep(1000);
        new Thread(()->{
            synchronized (room){
                hasCigarette=true;
                log.debug("烟到了哦");
                room.notify();
            }

        },"送烟的").start();
    }
}
